﻿
using Microsoft.CodeAnalysis;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DynamicCode.Compilation
{
    public sealed class DynamicCodeMultipleParameterBuilder : BaseDynamicCodeBuilder, IDynamicCodeBuilder
    {
        public DynamicCodeMultipleParameterBuilder(string codetemplate, DynamicCodeParameterInfo[] parameters) : base(codetemplate)
        {

            Parameters = parameters;
        }


        public DynamicCodeParameterInfo[] Parameters { get; private set; }

        public override Type[] Grenerate(DynamicCodeItem[] items)
        {
            var group = items.GroupBy(n => n.Code);
            foreach (var item in group)
            {
                if (item.Count() > 1) throw new DynamicCodeException($"Code[{item.Key}]重复");
            }

            string[] codeBuilder = items.Select(dBEntity =>
            {
                var variable = FindVariable(dBEntity.Content);

                Verification(variable);
                string content = Replace(dBEntity.Content, variable, true);

                var code = Replace(CodeTemplate, new Dictionary<string, string> { { "@code@", dBEntity.Code }, { "@content@", content } });
                return code;
            }).ToArray();

            var types = GenerateCode(codeBuilder);
            return types;
        }

        protected override IDictionary<string, string> FindVariable(string str)
        {
            var templates = new Dictionary<string, string>();
            int index = -1;
            bool isbegin = false;

            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == '@')
                {
                    if (!isbegin)
                    {
                        isbegin = true;
                        index = i;
                        continue;
                    }
                    isbegin = false;
                    var length = i - index;
                    var key = str.Substring(index, length + 1);
                    if (key == "@@") continue;
                    if (!templates.ContainsKey(key))
                    {
                        var indexkey = key.IndexOf(".");
                        if (indexkey == -1) throw new DynamicCodeException($"{key} 中的对像未批量对象");
                        var parameterName = key.Substring(1, indexkey - 1);
                        var propertyName = key.Substring(indexkey + 1, key.Length - 2 - indexkey);
                        var item = Parameters.FirstOrDefault(n => n.Name == parameterName);
                        if (item == null) throw new DynamicCodeException($"{key} 中的对像{parameterName}不存在");
                        var prop = item.ParameterType.GetProperty(propertyName);
                        if (prop == null) throw new DynamicCodeException($"{key} 中的对像{propertyName}不存在");
                        templates.Add(key, $"{str.Substring(index + 1, length - 1)}");
                    }
                }
            }

            return templates;
        }

    }
}
